﻿using HECore.Admin.WebApi.Models.Roles.Dtos;
using HECore.Tenant.Contract.Permissions;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using HECore.ServiceFrame.Attributes;
using HECore.ServiceFrame.Models;

namespace HECore.Admin.WebApi.Controllers
{
    /// <summary>
    /// 角色接口
    /// </summary>
    [Route("api/[controller]")]
    [ApiController]
    public class RoleController : ControllerBase
    {
        private readonly CurrentEmployee _currentEmployee;

        /// <summary>
        /// 依赖注入
        /// </summary>
        /// <param name="currentEmployee"></param>
        public RoleController(CurrentEmployee currentEmployee) { _currentEmployee = currentEmployee; }

        /// <summary>
        /// 获取角色功能树结构
        /// </summary>
        /// <returns></returns>
        [HttpGet("permission/tree")]
        [Audited]
        [EmoloyeeAuthorize(RolePermissions.Manage)]
        public List<RolePermissionTreeDto> GetPermissionTree()
        {
            var tree = new List<RolePermissionTreeDto>();

            // 角色管理
            var RoleManageTree = new RolePermissionTreeDto() { FunctionTitle = "角色管理", FunctionName = RolePermissions.Manage, children = new List<RolePermissionTreeDto>() };
            RoleManageTree.children.Add(new RolePermissionTreeDto() { FunctionTitle = "查看角色", FunctionName = RolePermissions.Select });
            RoleManageTree.children.Add(new RolePermissionTreeDto() { FunctionTitle = "添加角色", FunctionName = RolePermissions.Create });
            RoleManageTree.children.Add(new RolePermissionTreeDto() { FunctionTitle = "编辑角色", FunctionName = RolePermissions.Edit });
            RoleManageTree.children.Add(new RolePermissionTreeDto() { FunctionTitle = "删除角色", FunctionName = RolePermissions.Delete });
            tree.Add(RoleManageTree);

            // 员工管理
            var EmployeeManageTree = new RolePermissionTreeDto() { FunctionTitle = "员工管理", FunctionName = EmployeePermissions.Manage, children = new List<RolePermissionTreeDto>() };
            EmployeeManageTree.children.Add(new RolePermissionTreeDto() { FunctionTitle = "查看员工", FunctionName = EmployeePermissions.Select });
            EmployeeManageTree.children.Add(new RolePermissionTreeDto() { FunctionTitle = "添加员工", FunctionName = EmployeePermissions.Create });
            EmployeeManageTree.children.Add(new RolePermissionTreeDto() { FunctionTitle = "编辑员工", FunctionName = EmployeePermissions.Edit });
            EmployeeManageTree.children.Add(new RolePermissionTreeDto() { FunctionTitle = "删除员工", FunctionName = EmployeePermissions.Delete });
            tree.Add(EmployeeManageTree);

            if (_currentEmployee.IsAdministrator)
            {
                foreach (var item in tree)
                {
                    item.Selected = true;
                    foreach (var child in item.children)
                    {
                        child.Selected = true;
                    }
                }
            }

            return tree;
        }
    }
}